<template>
  <!-- 用户管理 -->
  <div class="userMannage">
    <ContentWrap>
      <FormContainer :column="4">
        <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
          <el-form-item prop="costomerName" label="顾客姓名">
            <el-input v-model="formData.costomerName" placeholder="请输入顾客姓名" clearable />
          </el-form-item>
          <el-form-item prop="costomerPhone" label="联系电话">
            <el-input v-model="formData.costomerPhone" placeholder="请输入联系电话" clearable />
          </el-form-item>
          <el-form-item prop="state" label="处理状态">
            <el-select v-model="formData.state" clearable>
              <el-option label="已处理" :value="1" />
              <el-option label="未处理" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item prop="submitDate" label="提报时间">
            <el-date-picker
              v-model="formData.submitDate"
              type="daterange"
              opt
              range-separator="-"
              format="yyyy-mm-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item
            :span="22"
            align="right"
          >
            <Button :loading="loading" @click="resetHandle('form')">重置</Button>
            <Button type="primary" enter :loading="loading" @click="queryHandle">查询</Button>
          </el-form-item>
        </el-form>
      </FormContainer>
      <table-box
        ref="table"
        :no-request="true"
        :other-filters="buyConsultParams"
        :table-config="tableConfig"
      />
    </ContentWrap>
  </div>
</template>
<script>
import ContentWrap from '@/components/ContentWrap'
import FormContainer from '@/components/FormContainer'
import Button from '@/components/Button'
import { ArrayFormat } from '@/utils/params-format'
import { buyConsultUpdate } from '@/api/website'
import { deepCopy, date24, parseTime } from '@/utils'
export default {
  name: 'WebsiteBuyConsult',
  components: {
    ContentWrap,
    FormContainer,
    Button
  },
  data() {
    return {
      formData: {
        costomerName: '',
        costomerPhone: '',
        state: '',
        submitDate: []
      },
      formRules: {
        costomerPhone: [
          { pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/, message: '请输入正确的联系电话', trigger: 'blur' }
        ]
      },
      loading: false,
      buyConsultParams: [],
      tableConfig: {
        interfacePath: '/call-center/api/v1/purchaseConsultation/page',
        pageFlag: true,
        hideFuncBar: true,
        sort: 'submitDate',
        order: 'desc',
        // 表格字段动态配置
        setting: [
          {
            prop: 'index',
            label: '序号',
            width: 80,
            align: 'center',
            formatData: function(val) { return val.keyIndex + 1 }
          },
          {
            prop: 'costomerName',
            label: '顾客姓名',
            align: 'center'
          },
          {
            prop: 'costomerPhone',
            label: '联系电话',
            isButton: true,
            className: 'table-click-text',
            clickFun: this.clickTel
          },
          {
            prop: 'address',
            label: '地址',
            align: 'center'
          },
          {
            prop: 'needs',
            label: '家具需求',
            align: 'center'
          },
          {
            prop: 'submitDate',
            label: '提报时间',
            align: 'center',
            formatData: function(val) {
              if (val.submitDate) return val.submitDate
              return '--'
            }
          },
          {
            label: '处理状态',
            align: 'center',
            render: (h, rows) => {
              return rows.state === 1 ? [h('span', '已处理')] : [h('Button', {
                attrs: {
                  type: 'primary'
                },
                on: {
                  click: this.handleCurrent.bind(this, rows)
                }
              }, '未处理')]
            }
          }

        ]
      }
    }
  },
  mounted() {},
  methods: {
    // 根据条件查询
    queryHandle() {
      this.loading = true
      this.$refs.form
        .validate()
        .then(() => {
          // FIXME:
          const data = deepCopy(this.formData)
          if (data.submitDate instanceof Array && data.submitDate.length === 2) {
            data.submitDate[0] = parseTime(data.submitDate[0])
            data.submitDate[1] = parseTime(date24(data.submitDate[1]))
          }
          this.buyConsultParams = [...ArrayFormat(data, { submitDate: ['>=', '<'] })]
          this.$nextTick(() => {
            this.$refs.table.initTableData()
          })
        })
        .finally(() => {
          this.loading = false
        })
    },
    /**
     * 处理信息是否处理状态
     */
    handleCurrent(rows) {
      const params = { ...rows }
      this.$msgbox({
        title: '提示',
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        message: '确定此信息已处理？'
      })
        .then(() => {
          params.state = 1
          buyConsultUpdate(params).then(res => {
            if (res.success === 1) {
              rows.state = 1
              this.$message({
                message: '处理成功',
                type: 'success'
              })
              this.$refs.table.initTableData()
            }
          })
        })
        .catch(e => e)
    },
    /**
     * 重置查询条件
     */
    resetHandle(formName) {
      this.$refs[formName].resetFields()
    },
    // 跳转到呼出
    clickTel(row) {
      this.$router.push({ name: 'CallOut', query: { callTel: row.customerPhone, name: row.customerName }})
    }
  }
}
</script>
<style lang='scss' module>
</style>
